package com.etone.jdbc;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.RowSet;

import junit.framework.TestCase;
public class SybaseJdbc extends TestCase {

	
	private static Connection conn ;
	
	static{
		try {
			Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
			String userName = "DBA";
			String pwd = "sql";
			String url = "jdbc:sybase:Tds:192.168.8.24:2639/asiqssmp";
			conn = DriverManager.getConnection(url, userName, pwd);
			conn.createStatement();
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e){
			e.printStackTrace();
		}
	}
	
	public void test1(){
		String sql = "select * from _jasic";
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.createStatement();
			if(stmt.execute(sql)){
				rs  =stmt.getResultSet();
				
				int count = rs.getMetaData().getColumnCount();
				StringBuilder sb = new StringBuilder();
				while(rs.next()){
					for(int i=1;i<count;i++){
						sb.append(rs.getString(i));
						sb.append("       ");
					}
					sb.append("\n");
				}
				System.out.println(sb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void test2(){
		String sql = "select top 1000 * from stbBR_CGI25_20110728";
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
				
			rs =stmt.executeQuery();
				int count = rs.getMetaData().getColumnCount();
				StringBuilder sb = new StringBuilder();
				rs.last();
				InputStream []in[] = new InputStream[rs.getRow()][count];
//				rs.beforeFirst();
//				rs.first();
				rs =stmt.executeQuery();
				int index = 0;
				while(rs.next()){
					for(int i=0;i<count;i++){
						System.out.println("SSSSSSSSSSSS" + rs.getRow());
						sb.append(rs.getString(i+1));
						sb.append("       ");
						in[index][i] = rs.getBinaryStream(i+1);
						rs.getBinaryStream(i+1);
					}
						index++;
					sb.append("\n");
				}
				System.out.println(sb);
//				
//				StringBuffer sb2 = new StringBuffer();
//				byte b[] = new byte[1024];
//				for(int k=0;k<in[1].length;k++){
//					try {
//						int l = in[1][k].read(b, 0, in[1].length);
//						System.out.println(new String(b,0,l,"GBK"));
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		new SybaseJdbc().test2();
		
	}
}
